﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Helpers;

namespace DataProcessModule.Protocol
{
    public class SoftwareVersionReport : BaseInstruction
    {
        #region param

        private string softwareVersion;//防火盒软件版本号 4 DWORD 例：18120301：01 14 7E 6D

        private int CANPotocolVersion;//当前CAN协议版本	1 BYTE 0：北京福田12米纯电动 1：--

        private Int32 Can1BaudRate;//CAN1波特率 4 DWORD	

        private Int32 Can2BaudRate;//CAN2波特率 4 DWORD	

        #endregion

        #region attributes

        public string AtsoftwareVersion { get { return softwareVersion; } }

        public int AtCANPotocolVersion { get { return CANPotocolVersion; } }

        public Int32 AtCan1BaudRate { get { return Can1BaudRate; } }

        public Int32 AtCan2BaudRate { get { return Can2BaudRate; } }

        #endregion

        #region construction

        public SoftwareVersionReport()
        {
            instHeadInfo.instID = 0x0117;
        }

        public SoftwareVersionReport(byte[] buf)
        {

            int offset = 0;
            // 获取头信息
            GetHeadInfo(buf, ref offset);
            BodyStreamSplit(buf, ref offset);
        }

        #endregion

        #region function

        public void BodyStreamSplit(byte[] buf, ref int offset)
        {
            try
            {
                softwareVersion  = BC.ToBCDString(buf, ref offset,4);
                CANPotocolVersion = buf[offset++];
                Can1BaudRate = BC.ToInt32Net(buf, ref offset);
                Can2BaudRate = BC.ToInt32Net(buf, ref offset);
            }
            catch (Exception e) { }
        }

        public string CombinedSqlString()
        {
            string _sql = string.Empty;
            _sql = "INSERT INTO table_firebox_version_upload (TerminalKey,softwareVersion," +
                "CANPotocolVersion,Can1BaudRate,Can2BaudRate) VALUES('" +
                instHeadInfo.tdPhoneNumber + "','" + softwareVersion + "'," +
                CANPotocolVersion + ',' + Can1BaudRate + ',' + Can2BaudRate +
                ") ON DUPLICATE KEY UPDATE softwareVersion=VALUES(softwareVersion)" +
                ",softwareVersion=VALUES(softwareVersion),Can1BaudRate=VALUES(Can1BaudRate)" +
                ",Can2BaudRate=VALUES(Can2BaudRate);";
            return _sql;
        }
        public string InsertString()
        {
            string _sql = string.Empty;
            _sql = "INSERT INTO table_firebox_version_upload_" + DateTime.Now.ToString("yyyyMMdd") + " (TerminalKey,softwareVersion," +
                "CANPotocolVersion,Can1BaudRate,Can2BaudRate) VALUES('" +
                instHeadInfo.tdPhoneNumber + "','" + softwareVersion + "'," +
                CANPotocolVersion + ',' + Can1BaudRate + ',' + Can2BaudRate +
                ");";
            return _sql;
        }
        #endregion
    }
}
